﻿using System;
using System.Diagnostics;

namespace Utilities
{
    /// <summary>
    /// Verifies a postcondition
    /// </summary>
    public static class PostCondition
    {
        /// <summary>
        /// Throws an invalid operation exception when the condition does not meet the expected output
        /// </summary>
        /// <param name="condition">The condition to satisfy</param>
        /// <param name="message">The message to pass to the exception</param>
        /// <param name="traceSource">The source to log the error</param>
        public static void Ensure(bool condition, string message, TraceSource traceSource = null)
        {
            if (!condition)
            {
                if (traceSource != null)
                {
                    traceSource.LogError(message);
                }

                throw new InvalidOperationException(message);
            }
        }

        /// <summary>
        ///  Throws an invalid operation exception when the code flow reaches the call
        /// </summary>
        /// <param name="message"></param>
        public static void InvalidScope(string format, params object[] args)
        {
            throw new InvalidOperationException(string.Format(format, args));
        }
    }
}
